using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Robot.Business;
using Robot.Data;
using Robot.Entities;
using DevExpress.Web.ASPxGridView;
using Scb.Framework;
using System.IO;
using WebRobot.App_code;

namespace WebRobot.Pages
{
  public partial class ApprovedPage : BasePage
  {

    #region Event

    protected void Page_Load(object sender, EventArgs e)
    {


      try
      {
        if (CurrentUser.Role.Value != 2) // not checker
        {
          Response.Redirect("ErrorPage.aspx");
        }

        DateTime from = DateTime.Today;
        DateTime to = DateTime.Now;

        gvViewTran.DataSource = tblImportTransactionService.GetImportTransactionSubmited(from, to);
        gvViewTran.DataBind();
        gvViewTran.Visible = true;

      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.ApprovedPage::Page_Load", ex);
      }

    }

    protected void btnCheckAll_Click(object sender, EventArgs e)
    {
      gvViewTran.Selection.SelectAll();
    }

    protected void btnUnCheckALL_Click(object sender, EventArgs e)
    {
      gvViewTran.Selection.UnselectAll();

    }

    protected void btnApproved_Click(object sender, EventArgs e)
    {
      try
      {
        if (gvViewTran.DataSource != null)
        {
          tblImportTransactionCollection list = new tblImportTransactionCollection();
          list.AddRange((tblImportTransactionCollection)gvViewTran.DataSource);
          int countAppr = 0;
          for (int i = 0; i < gvViewTran.VisibleRowCount; i++)
          {
            if (gvViewTran.Selection.IsRowSelected(i))
            {
              tblImportTransaction tran = (tblImportTransaction)gvViewTran.GetRow(i);
              tblOpicsTransaction opicsTran = new tblOpicsTransaction();
              list.Remove(tran);
              if (tran.Branch.CompareTo("100") == 0 || tran.Branch.CompareTo("200") == 0 || tran.Branch.CompareTo("101") == 0)
              {
                opicsTran.Branch = "29";
              }
              else
              {
                opicsTran.Branch = "30";
              }

              opicsTran.BuyCurrency = tran.BuyCurrency;
              opicsTran.BuyAmount = tran.BuyAmount;
              opicsTran.CustomerRate = tran.CusRate;
              opicsTran.SellCurrency = tran.SellCurrency;
              opicsTran.SellAmount = tran.SellAmount;
              opicsTran.InternalRate = tblInternalRateService.GetInternalRateByCurrency(tran.BuyCurrency, tran.SellCurrency).InternalRate;
              opicsTran.ValueDate = tran.ValueDate;
              opicsTran.ImportTransactionID = tran.ID;
              opicsTran.Customer = GetOpicsCode(tran.CustomerName, tran.Branch);
              opicsTran.Porfolio = GetPortfolio(tran.CustomerName, tran.Branch);
              opicsTran.DealDate = DateTime.Today.Date;
              opicsTran.Status = 1; //ready

              tblOpicsTransactionService.CreatetblOpicsTransaction(opicsTran);
              tran = tblImportTransactionService.GettblImportTransaction(tran.ID);

              tran.Status = 3; // Approved
              tran.ApprovedBy = CurrentUser.Peoplewise;
              tran.ApprovedDate = DateTime.Now;
              tran.Command = "";

              tblImportTransactionService.UpdatetblImportTransaction(tran);
              countAppr++;
            }
          }
          gvViewTran.DataSource = list;
          gvViewTran.DataBind();
          lbStatus.InnerHtml = "Approved successful " + countAppr.ToString() + " transaction!";
          lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Blue");
        }
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ApprovedPage::btnApproved_Click", ex);

        lbStatus.InnerHtml = "Approved error!";
        lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Red");
      }
    }

    protected void gvViewTran_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
    {
      if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.EmptyDataRow && e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Group)
      {
        if (e.Row.Cells.Count > 8 && e.GetValue("Rule2") != null)
        {

          if (e.GetValue("Rule2").ToString() == "0" || e.GetValue("Rule3").ToString() == "0" || e.GetValue("Rule4").ToString() == "0" || e.GetValue("Rule5").ToString() == "0")
          {
            e.Row.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
          }
        }

      }
    }

    #region Event in Popup

    protected void btnOk_Click(object sender, EventArgs e)
    {
      try
      {
        if (gvViewTran.DataSource != null)
        {
          tblImportTransactionCollection list = new tblImportTransactionCollection();
          list.AddRange((tblImportTransactionCollection)gvViewTran.DataSource);

          int countReject = 0;

          for (int i = 0; i < gvViewTran.VisibleRowCount; i++)
          {
            if (gvViewTran.Selection.IsRowSelected(i))
            {
              tblImportTransaction tran = (tblImportTransaction)gvViewTran.GetRow(i);
              list.Remove(tran);
              tran = tblImportTransactionService.GettblImportTransaction(tran.ID);

              tran.Command = txtCommand.Text;
              tran.Status = 2; //reject

              tblImportTransactionService.UpdatetblImportTransaction(tran);
              countReject++;
            }

          }
          gvViewTran.DataSource = list;
          gvViewTran.DataBind();
          lbStatus.InnerHtml = "Reject successful " + countReject.ToString() + "transaction!";
          lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Blue");
        }
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ApprovedPage::btnOK_Click", ex);
      }
    }

    #endregion

    #endregion

    #region Helper

    private string GetOpicsCode(string CustomerName, string sBranch)
    {
      if (CustomerName == "")
      {
        CustomerName = "Wholesale Banking";
      }
      string opicsCode = "";
      opicsCode = tblOpicCodeService.GettblOpicCode(CustomerName, sBranch).OpicsCode;
      return opicsCode;
    }

    private string GetPortfolio(string CustomerName, string sBranch)
    {
      if (CustomerName == "")
      {
        CustomerName = "Wholesale Banking";
      }
      string opicsCode = "";
      opicsCode = tblOpicCodeService.GettblOpicCode(CustomerName, sBranch).Portfolio;
      return opicsCode;
    }

    #endregion



  }
}